﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc.Ajax;

namespace System.Web.Mvc.Html
{

    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString BootStarpStyleBtn(this HtmlHelper htmlHelper, string text, string ico = "", object htmlAttributes = null, string modeDialogTargetId = "")
        {
            return GetHtml("button", ico, text, "", htmlAttributes, modeDialogTargetId);
        }
        public static MvcHtmlString BootStarpBtnStylelink(this HtmlHelper htmlHelper, string text, string ico = "", string link = "#", object htmlAttributes = null, string modeDialogTargetId = "")
        {
            return GetHtml("a", ico, text, link, htmlAttributes, modeDialogTargetId);
        }

        public static MvcHtmlString BootStarpLable(this HtmlHelper htmlHelper, object text, string style = "")
        {
            var builder = new TagBuilder("span");
            builder.AddCssClass("label");
            if (!string.IsNullOrEmpty(style))
                builder.AddCssClass(style);
            builder.InnerHtml = text.ToString();
            return MvcHtmlString.Create(builder.ToString());
        }
        public static MvcHtmlString BootStarpBadge(this HtmlHelper htmlHelper, object text, string style = "")
        {
            var builder = new TagBuilder("span");
            builder.AddCssClass("badge");
            if (!string.IsNullOrEmpty(style))
                builder.AddCssClass(style);
            builder.InnerHtml = text.ToString();
            return MvcHtmlString.Create(builder.ToString());
        }


        static MvcHtmlString GetHtml(string tag, string ico, string text, string link = "#", object htmlAttributes = null, string modeDialogTargetId = "")
        {
            var builder = new TagBuilder(tag);
            if (!string.IsNullOrEmpty(link))
            {
                builder.MergeAttribute("href", link);
            }
            if (!string.IsNullOrEmpty(modeDialogTargetId))
            {
                builder.MergeAttribute("data-toggle", "modal");
                builder.MergeAttribute("data-target", "#" + modeDialogTargetId);
            }
            if (!string.IsNullOrEmpty(ico))
            {
                var image = new TagBuilder("i");
                image.AddCssClass(ico);
                text = MvcHtmlString.Create(image.ToString()).ToHtmlString() + text;
            }
            builder.InnerHtml = text;
            if (htmlAttributes != null)
                builder.MergeAttributes(new System.Web.Routing.RouteValueDictionary(htmlAttributes));
            else
            {
                builder.MergeAttribute("class", "btn");
            }
            return MvcHtmlString.Create(builder.ToString());
        }

        /// <summary>
        /// 去的Action
        /// </summary>
        /// <typeparam name="TController"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        public static string ActionName<TController>(this HtmlHelper htmlHelper, Expression<Action<TController>> operation)
          where TController : Controller
        {
            var actionName = ((MethodCallExpression)operation.Body).Method.Name;
            return actionName;
        }

        /// <summary>
        /// 生成Img标记
        /// 如: " Html.Image("img1", ResolveUrl("~/Content/XBox.jpg"), "XBox Console", new {border="4px"})"
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id"></param>
        /// <param name="url"></param>
        /// <param name="alternateText"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes = null)
        {
            var builder = new TagBuilder("img");

            builder.GenerateId(id);

            builder.MergeAttribute("src", url);
            builder.MergeAttribute("alt", alternateText);
            builder.MergeAttributes(new System.Web.Routing.RouteValueDictionary(htmlAttributes));

            return new MvcHtmlString(builder.ToString(TagRenderMode.SelfClosing));
        }

        //TODO:
        public static MvcHtmlString Text(this HtmlHelper help, ICollection<string> list, string id, string cssclass, object htmlAttributes = null)
        {
            var builder = new TagBuilder("span");
            builder.GenerateId(id);
            builder.AddCssClass(cssclass);

            StringBuilder strText = new StringBuilder();
            list.ToList().ForEach(a => strText.AppendFormat("{0}<br/>", a));

            builder.InnerHtml = strText.ToString();

            builder.MergeAttributes(new System.Web.Routing.RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(builder.ToString());
        }

        /// <summary>
        /// Change MVC validation message show to float 
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="validationMessage"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static MvcHtmlString FloatValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string validationMessage = null, string message = null)
        {

            string modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(string.Format("<div class='FloatValidation' id='FloatValidation-{0}'>", modelName));
            stringBuilder.AppendLine("<span class='tipArrow'> </span>");
            stringBuilder.AppendLine("<div class='tipContent'>");
            //stringBuilder.AppendLine("<p>" + message + "</p>");
            string validationMessageForString = ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToString();
            stringBuilder.AppendLine(validationMessageForString);
            stringBuilder.AppendLine("</div>");
            stringBuilder.AppendLine("</div>");
            return new MvcHtmlString(stringBuilder.ToString());
        }


        /// <summary>
        /// Convert string to mvc html string 
        /// </summary>
        /// <param name="htmlstring"></param>
        /// <returns></returns>
        public static MvcHtmlString ToHtmlString(this string htmlstring)
        {
            return new MvcHtmlString(htmlstring);
        }

    }
}